import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { Observable, throwError, of } from 'rxjs';
import { LoggerService } from './logger.service';
import { tap, finalize, catchError } from 'rxjs/operators';

class Errorhandler {
  static handleCatchError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error('An error occurred:', error.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.error(
        `Backend returned code ${error.status}, ` +
        + `error message was: ${error.message}`
        // + `body was: ${error.error}`
        );
    }
    return throwError(error);
  }
  static handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error('An error occurred:', error.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.error(
        `Backend returned code ${error.status}, `
        + `error message was: ${error.message}`
        // + `body was: ${error.error}`
        );
    }
  }
}
@Injectable()
export class TestInterceptor1 implements HttpInterceptor {
  constructor(private logger: LoggerService) {}
  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
      let ok: string;
      const newReq = req.clone();
      const started = Date.now();
      return next.handle(newReq).pipe(
        tap(data => {
          ok = event instanceof HttpResponse ? 'succeeded' : '';
        }, error => {
          ok = 'failured';
          Errorhandler.handleError(error);
        }),
        // catchError(Errorhandler.handleCatchError),
        finalize(() => {
          const elapsed = Date.now() - started;
          const msg = `testinterceptor1 ${newReq.method} "${newReq.urlWithParams}"
            ${ok} in ${elapsed} ms.`;
          this.logger.log(msg);
      }));
  }
}
@Injectable()
export class TestInterceptor2 implements HttpInterceptor {
  constructor(private logger: LoggerService) {}
  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
      let ok: string;
      const newReq = req.clone();
      const started = Date.now();
      return next.handle(newReq).pipe(
        tap(data => {
          ok = event instanceof HttpResponse ? 'succeeded' : '';
        }, error => {
          ok = 'failured';
          Errorhandler.handleError(error);
        }),
        // catchError(Errorhandler.handleCatchError),
        finalize(() => {
          const elapsed = Date.now() - started;
            const msg = `testinterceptor2 ${newReq.method} "${newReq.urlWithParams}"
              ${ok} in ${elapsed} ms.`;
          this.logger.log(msg);
      }));
  }
}
